// pages/upload/upload.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    image: []
  },

  loadImage() {
    wx.showLoading({
      title: '正在加载',
    })
    wx.chooseImage({
      success: (res) => {
        let {image} = this.data
        res.tempFilePaths.forEach(item => {
          image.unshift(item)
        })
        console.log(image);
        this.setData({
          image
        })
        
      },
      complete: ()  =>{
        wx.hideLoading()
      }
    }) 
  },

  previewImage(e) {
    let {index} = e.currentTarget.dataset
    let {image} = this.data
    let current = image.find((item,i) => {
      if(i == index) return item
    })
    wx.previewImage({
      urls: this.data.image,
      current,
    })
  },

  uploadImage() {
    wx.chooseImage({
      success: (res) => {
        let img = res.tempFilePaths
        wx.showLoading({
          title: '正在上传中',
        })
        wx.uploadFile({
          filePath: img[0],
          name: 'file',
          url: 'http://www.circlesport.cn/index/index/file',
          success: (res) => {
            let {src} = JSON.parse(res.data)
            src = 'http://www.circlesport.cn' + src
            let {image} = this.data
            image.unshift(src)
            this.setData({
              image
            })
          },
          complete: () => {
            wx.hideLoading()
          }
        })
      }
    })
  }


})